<?php 

require('../../configs/include.php');
class c_admin_control_producto extends super_controller {

	public function add()
	{
		$producto = new producto($this->post);$producto->exceptions();
		$this->orm->connect();
		$this->orm->insert_data("normal",$producto);
		$this->orm->close();
		
		$this->temp_aux = 'admin/message.tpl';
		$this->engine->assign('type_warning','success');
		$this->engine->assign('msg_warning',$this->gvar['m_correct_insert']);
	}
	
	public function delete()
	{
		$producto = new producto($this->post);
		$this->orm->connect();
		$this->orm->delete_data("normal",$producto);
		$this->orm->close();
		
		$this->temp_aux = 'admin/message.tpl';
		$this->engine->assign('type_warning','success');
		$this->engine->assign('msg_warning',$this->gvar['m_correct_delete']);
	}
	
	public function edit()
	{
		$producto = new producto($this->post);
		$producto->auxiliars=$producto->pre_edit();
		$this->engine->assign('object',$producto);
		$this->temp_aux = 'admin/control/producto_edit.tpl';
	}
	
	public function save()
	{
		$producto = new producto($this->post);
		$producto->auxiliars=unserialize(base64_decode($this->post->auxiliars));
		$producto->exceptions();
		$this->orm->connect();
		$this->orm->update_data("normal",$producto);
		$this->orm->close();
				
		$this->temp_aux = 'admin/message.tpl';
		$this->engine->assign('type_warning','success'); 
		$this->engine->assign('msg_warning',$this->gvar['m_correct_update']);
	}
	
	public function display()
	{		
		if(!(($this->get->option=="save" && $this->error==1)|| $this->get->option=="edit"))
		{
			$cod['producto']['page']=$this->get->page;
			$cod['producto']['order']=$this->get->order; $cod['producto']['show_order']=$this->get->show_order;
			$cod['producto']['search_type']=$this->get->search_type; $cod['producto']['search_words']=$this->get->search_words;
			$options['producto']['lvl2']="by_pages";
			
			$this->orm->connect();
			$this->orm->read_data(array("producto"),$options,$cod);
			$producto = $this->orm->get_objects("producto");
			$this->engine->assign('total_page',$this->orm->numpages['producto']);
			
			if($this->get->page > $this->orm->numpages['producto']){$this->engine->assign('actual_page',$this->orm->numpages['producto']);}
			else{$this->engine->assign('actual_page',$this->get->page);}
			$this->orm->close();
			$this->engine->assign('results',$producto);
		}
		
		$this->engine->assign('title','producto');
		$this->engine->assign('obj_aux',new producto());
		$this->engine->assign('l_this',basename($_SERVER['PHP_SELF']));
		$this->engine->display('admin/header.tpl');
		$this->engine->display($this->temp_aux);
		
		if($this->error == 0)
		{
			if($this->temp_aux == 'admin/message.tpl'){$this->engine->display('admin/control/producto.tpl');}
		}
		else
		{
			if($this->get->option == "save")
			{
				$producto_one = new producto($this->post);
				$producto_one->auxiliars=$this->post->auxiliars;
				$this->engine->assign('object',$producto_one);
				$this->engine->display('admin/control/producto_edit.tpl');
			}
			else if($this->get->option == "add")
			{
				$this->engine->assign('object',$this->post);
				$this->engine->display('admin/control/producto.tpl');
			}
		}
		$this->engine->display('admin/footer.tpl');
	}
	
	public function run()
	{
		$this->restricted_zone();
		if(!isset($this->get->page) || $this->get->page<1){$this->get->page=1;}	
		$this->temp_aux = 'admin/control/producto.tpl';
		
		try {if (isset($this->get->option)){$this->{$this->get->option}();}}
		catch (Exception $e) {$this->error=1; $this->msg_warning=$e->getMessage(); $this->temp_aux = 'admin/message.tpl';
		$this->engine->assign('type_warning',$this->type_warning); $this->engine->assign('msg_warning',$this->msg_warning);}
		
		$this->display();
	}
}

$call=new c_admin_control_producto();
$call->run();

?>